package com.ajk.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class CacheConfiguration {

    @Autowired
    Environment env;

    
    @Bean
    public JedisPoolConfig redisPoolConfig() {
        final JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setMinEvictableIdleTimeMillis(60000);
        jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(1800000);
        jedisPoolConfig.setNumTestsPerEvictionRun(-1);
        jedisPoolConfig.setTestOnReturn(false);
        jedisPoolConfig.setTestWhileIdle(true);
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
        return jedisPoolConfig;
    }
   
    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName(env.getProperty("cache.host"));
        //  public static final int DEFAULT_TIMEOUT = 2000;
        JedisPoolConfig poolConfig = redisPoolConfig();
        factory.setPoolConfig(poolConfig);
        factory.setTimeout(5000);
        factory.setUsePool(true);
        factory.setPassword(env.getProperty("cache.passwd"));
        return factory;
    }
    
//    @Bean
//    public JedisConnectionFactory jedisConnectionFactory() {
//        JedisConnectionFactory factory = new JedisConnectionFactory();
//        factory.setHostName(env.getProperty("cache.host"));
//        //  public static final int DEFAULT_TIMEOUT = 2000;
//        JedisPoolConfig poolConfig = factory.getPoolConfig();
//        if(poolConfig!=null){
//        	
//        	//在获取连接的时候检查有效性, 默认false
//        	poolConfig.setTestOnBorrow(true);
//        	 
//        	//在空闲时检查有效性, 默认false
//        	poolConfig.setTestWhileIdle(true);
//        	
//        	poolConfig.setTestOnReturn(true);
//        }
//        factory.setTimeout(5000);
//        factory.setUsePool(true);
//        factory.setPassword(env.getProperty("cache.passwd"));
//        return factory;
//    }

    @Bean
    public StringRedisSerializer stringRedisSerializer() {
        return new StringRedisSerializer();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(stringRedisSerializer());
        redisTemplate.setHashKeySerializer(stringRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager() {
        return new RedisCacheManager(redisTemplate());
    }
}